﻿
// First: Create config
ko.validation.configure({
	registerExtenders: true,
	messagesOnModified: true,
	insertMessages: true,
	parseInputAttributes: true,
	messageTemplate: null,
	decorateElement: true,
	errorClass: "error",
	errorMessageClass: "error-desc"
});

// Second: Create new model to apply to view
var viewModel = new function () {
    // About you
    this.Title = ko.observable(data.BannerInfo.Title).extend({ maxLength: 250 }).extend({ required: { message: "Tên quảng bá là bắt buộc"} });
    this.ShortDesc = ko.observable(data.BannerInfo.ShortDesc);
    this.LongDesc = ko.observable(data.BannerInfo.LongDesc);
    this.ImageURL = ko.observable("/Upload/Banners/" + data.BannerInfo.ImageURL);
    this.FlashURL = ko.observable();
    this.VideoURL = ko.observable();
    this.HTML = ko.observable(data.BannerInfo.HTML);
    this.Approved = ko.observable(data.BannerInfo.Approved);
    this.Active = ko.observable(data.BannerInfo.Active);
    this.ImageFileName = ko.observable(data.BannerInfo.ImageURL);
    this.FlashFileName = ko.observable(data.BannerInfo.FlashURL);
    this.VideoFileName = ko.observable(data.BannerInfo.VideoURL);

    // Submit, Next button
    this.InvalidateForm = ko.observable(false);
    this.SubmitForm = function () {
        if (viewModel.errors().length == 0) {
            $('form').submit();
        }
        else {
            viewModel.errors.showAllMessages();
            this.InvalidateForm(true);
        }
    }
};

viewModel.errors = ko.validation.group(viewModel);

// Third: Apply view model to view to handle action when user input data
ko.validation.rules.pattern.message = 'Invalid.';
ko.applyBindings(viewModel, document.getElementById("main"));